package com.example.smartpark.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.smartpark.common.Result;
import com.example.smartpark.dto.AnnouncementDTO;
import com.example.smartpark.dto.AnnouncementQueryDTO;
import com.example.smartpark.service.AnnouncementService;
import com.example.smartpark.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 管理员公告Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/announcements")
public class AdminAnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 管理员获取公告列表
     *
     * @param queryDTO 查询条件
     * @return 公告列表
     */
    @GetMapping
    public Result<IPage<AnnouncementDTO>> getAnnouncementList(AnnouncementQueryDTO queryDTO) {
        log.info("管理员获取公告列表: {}", queryDTO);
        IPage<AnnouncementDTO> page = announcementService.getAdminAnnouncementPage(queryDTO);
        return Result.success(page);
    }

    /**
     * 创建公告
     *
     * @param announcementDTO 公告信息
     * @param token 用户token
     * @return 创建后的公告
     */
    @PostMapping
    public Result<AnnouncementDTO> createAnnouncement(
            @RequestBody @Valid AnnouncementDTO announcementDTO,
            @RequestHeader("Authorization") String token) {
        log.info("创建公告: {}", announcementDTO);
        
        // 设置发布者ID
        Integer publisherId = JwtUtils.getUserIdFromToken(token.replace("Bearer ", ""));
        announcementDTO.setPublisherId(publisherId);
        
        AnnouncementDTO announcement = announcementService.createAnnouncement(announcementDTO);
        return Result.success(announcement);
    }

    /**
     * 更新公告
     *
     * @param id 公告ID
     * @param announcementDTO 公告信息
     * @return 更新后的公告
     */
    @PutMapping("/{id}")
    public Result<AnnouncementDTO> updateAnnouncement(
            @PathVariable Long id,
            @RequestBody @Valid AnnouncementDTO announcementDTO) {
        log.info("更新公告: id={}, announcement={}", id, announcementDTO);
        AnnouncementDTO announcement = announcementService.updateAnnouncement(id, announcementDTO);
        return Result.success(announcement);
    }

    /**
     * 删除公告
     *
     * @param id 公告ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteAnnouncement(@PathVariable Long id) {
        log.info("删除公告: {}", id);
        boolean result = announcementService.deleteAnnouncement(id);
        return Result.success(result);
    }

    /**
     * 更改公告状态
     *
     * @param id 公告ID
     * @param status 状态
     * @return 更新后的公告
     */
    @PutMapping("/{id}/status")
    public Result<AnnouncementDTO> changeAnnouncementStatus(
            @PathVariable Long id,
            @RequestBody Integer status) {
        log.info("更改公告状态: id={}, status={}", id, status);
        AnnouncementDTO announcement = announcementService.changeAnnouncementStatus(id, status);
        return Result.success(announcement);
    }
} 